<!--
 * @Author: aster lipian1004@163.com
 * @Date: 2025-02-20 17:09:49
 * @FilePath: \aster-flowable-uniapp\src\components\form\render\geo-location.vue
 * @Description: 地理位置
 * Copyright (c) 2025 by Aster, All Rights Reserved.
-->
<template>
  <van-field
    v-if="!_hidden"
    :name="formItemProp"
    :readonly="true"
    :required="_required"
    :rules="_rules"
    :input-align="FieldInputAlignEnum.RIGHT"
    :error-message-align="FieldInputAlignEnum.RIGHT"
    :label-width="showLabel ? '' : '5rpx'"
    :placeholder="_placeholder"
  >
    <template #label>
      <text v-if="showLabel">{{ formItem.title }}</text>
    </template>
    <template #extra>
      <view class="w-full flex justify-end items-center pt-20rpx" v-if="isNotEmpty(_address)">
        {{ _address }}
        <i class="iconfont icon-guanbi1 pl-20rpx" v-if="!_readonly" @click="handleClear"></i>
      </view>
    </template>
    <template #button>
      <i v-if="formItem.props.readonly" class="iconfont icon-dingwei"></i>
      <van-button v-else size="small" type="primary" icon="location-o" @click="handleOpen" />
    </template>
  </van-field>

  <aster-amap ref="amapRef" :form="_value" @address="handleAddress" />
</template>
<script setup lang="ts">
  import { evaluateFormula } from '@/utils/workflow';
  import { computed, PropType, ref } from 'vue';
  import { isNotEmpty } from '@/utils/is';
  import { FormPermissionEnum } from '@/enums/workFlowEnum';
  import { FieldInputAlignEnum } from '@/enums/formEnum';
  import AsterAmap from '@/components/common/aster-amap.vue';

  const emit = defineEmits(['update:value']);
  const props = defineProps({
    value: {
      type: Object as PropType<WorkForm.GeoPositionModel>,
      default: {},
    },
    mode: {
      type: String as PropType<'design' | 'form' | 'search' | 'table' | 'print'>,
      default: 'design',
    },
    formData: {
      type: Object as PropType<WorkForm.FormDataModel>,
      default: {},
    },
    formItem: {
      type: Object as PropType<WorkComponent.ComponentConfig>,
      default: {},
    },
    tableId: {
      type: String,
      default: '',
    },
    tableIndex: {
      type: Number,
      default: 0,
    },
    showLabel: {
      type: Boolean,
      default: true,
    },
  });

  // 注册组件
  const amapRef = ref();

  // 清除地图
  const handleClear = () => {
    _value.value = {
      address: '',
      longitude: 0,
      latitude: 0,
      adcode: '',
    };
  };

  /**
   * @description: 获取地图地址信息
   * @param {*} address 地址
   * @param {*} coord 经纬度
   * @param {*} adcode 区域编码
   * @return {*}
   */
  const handleAddress = (address: string, coord: number[], adcode: string) => {
    _value.value['address'] = address;
    _value.value['longitude'] = coord[0];
    _value.value['latitude'] = coord[1];
    _value.value['adcode'] = adcode;
  };

  // 打开弹出层
  const handleOpen = () => {
    if (!_readonly.value) {
      amapRef.value.init();
    }
  };

  // 键
  const formItemProp = computed(() => {
    if (isNotEmpty(props.tableId)) {
      return props.tableId + '.' + props.tableIndex + '.' + props.formItem.id;
    } else {
      return props.formItem.id;
    }
  });

  // 地址
  const _address = computed(() => {
    return _value.value && _value.value.address ? _value.value.address : '';
  });

  /**
   * @description: 提示词
   */
  const _placeholder = computed(() => {
    if (_readonly.value) {
      return '';
    }
    return isNotEmpty(_address.value) ? '' : '请选择';
  });

  /**
   * @description: 组件值
   */
  const _value = computed({
    get() {
      return props.value;
    },
    set(val) {
      emit('update:value', val);
    },
  });

  /**
   * @description: 是否隐藏, true-隐藏
   */
  const _hidden = computed(() => {
    let r = false;
    // 解析隐藏条件公式
    if (props.formItem.props.hidden) {
      let expression = props.formItem.props.hidden;
      // 如果是子表中的控件，则需要用到下标
      if (isNotEmpty(props.tableId)) {
        expression = expression.replaceAll('?', props.tableIndex);
      }
      r = evaluateFormula(expression, props.formData);
    }
    // 判断流程节点下该控件是否隐藏
    if (props.formItem.operation && props.formItem.operation.length > 0) {
      r = r || props.formItem.operation[0] == FormPermissionEnum.HIDDEN;
    }
    return r;
  });

  /**
   * @description: 是否必填, true-必填
   */
  const _required = computed(() => {
    return props.formItem.props.required;
  });

  /**
   * @description: 校验规则
   */
  const _rules = computed(() => {
    const isRequired = _required.value;
    if (props.mode != 'form') {
      return [];
    }
    return isRequired ? [{ required: true, message: `请输入${props.formItem.title}` }] : [];
  });

  /**
   * @description: 是否只读, true-只读
   */
  const _readonly = computed(() => {
    let r = props.formItem.props.readonly;
    if (props.formItem.operation && props.formItem.operation.length > 0) {
      r = r || props.formItem.operation[0] == FormPermissionEnum.READONLY;
    }
    return r;
  });

  defineExpose({
    _hidden,
  });
</script>
<style scoped lang="scss"></style>
